home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / pcl4c60.zip / SIMPLE.C < prev    next >
Text File  |  1996-10-25  |  3KB  |  127 lines

  1. /* simple.c */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <malloc.h>
  6. #include <dos.h>
  7. #include <string.h>
  8. #include <conio.h>
  9.  
  10. #include "pcl4c.h"
  11.  
  12. #define FALSE 0
  13. #define TRUE !FALSE
  14.  
  15. /*** Global Variables ***/
  16.  
  17. int Port = COM1;          /* Port COM1 */
  18. int BaudCode;             /* baud rate code ( index into BaudRate[] ) */
  19. char *BaudRate[10] =  {"300","600","1200","2400","4800","9600",
  20.                        "19200","38400","57600","115200"};
  21. char RxBuffer[1024+16];
  22. char TxBuffer[128+16];
  23.  
  24. /*** local prototypes */
  25.  
  26. int BaudMatch(char *);
  27. int ErrorCheck(int);
  28.  
  29. /*** main ***/
  30.  
  31. void main(int argc, char *argv[])
  32. {char c;
  33.  int  i, rc;
  34.  char far *Ptr;
  35.  int  Seg;
  36.  if(argc!=3)
  37.    {printf("Usage: SIMPLE <port> <baud>\n");
  38.     exit(1);
  39.    }
  40.  /* get port number from command line */
  41.  Port = atoi(argv[1]) - 1;
  42.  if((Port<COM1) || (Port>COM20))
  43.      {printf("Port must be COM1 to COM20\n");
  44.       exit(1);
  45.      }
  46.  /* get baud rate from command line */
  47.  BaudCode = BaudMatch(argv[2]);
  48.  if(BaudCode<0)
  49.      {printf("Cannot recognize baud rate = %s\n",argv[2]);
  50.       exit(1);
  51.      }
  52.  /* setup 1024 byte receive buffer */
  53.  Ptr = (char far *)RxBuffer;
  54.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  55.  ErrorCheck( SioRxBuf(Port,Seg,Size1024) );
  56.  /* setup 128 byte transmit buffer */
  57.  Ptr = (char far *)TxBuffer;
  58.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  59.  ErrorCheck( SioTxBuf(Port,Seg,Size128) );
  60.  /* set port parmameters */
  61.  ErrorCheck( SioParms(Port,NoParity,OneStopBit,WordLength8) );
  62.  /* reset the port */
  63.  ErrorCheck( SioReset(Port,BaudCode) );
  64.  /* set DTR and RTS */
  65.  ErrorCheck( SioDTR(Port,'S') );
  66.  ErrorCheck( SioRTS(Port,'S') );
  67.  
  68. #if 0
  69.  /* set RTS/CTS flow control */
  70.  ErrorCheck( SioFlow(Port,18) );
  71.  puts("Flow control on");
  72. #endif
  73.  
  74.  /* Set FIFO level */
  75.  if(SioFIFO(Port,LEVEL_8)) printf("[16550]\n");
  76.  else printf("[8250/16450]");
  77.  
  78.  printf("\nEnter terminal loop ( Type ^Z to exit )\n");
  79.  /* enter terminal loop */
  80.  while(TRUE)
  81.      {if(SioBrkKey())
  82.         {/* restore COM port status & exit */
  83.          printf("BREAK\n");
  84.          SioDone(Port);
  85.          exit(2);
  86.         }
  87.       /* check for data overrun */
  88.       if(SioLine(Port)&OverrunError) puts("Overrun!");
  89.       /* was key pressed ? */
  90.       if(kbhit())
  91.           {i = getch();
  92.            if((char)i==0x1a)
  93.               {/* restore COM port status & exit */
  94.                SioDone(Port);
  95.                exit(1);
  96.               }
  97.            else
  98.               {
  99.                SioPutc(Port,(char)i);
  100.               }
  101.           } /* end if */
  102.       /* any incoming over serial port ? */
  103.       i = SioGetc(Port,0);
  104.       if(i>-1) putch((char)i);
  105.       if(SioBrkSig(Port,'D')) printf("[BREAK detected]");
  106.      } /* end while */
  107. } /* end main */
  108.  
  109. int ErrorCheck(int Code)
  110. {/* trap PCL error codes */
  111.  if(Code<0)
  112.      {printf("ERROR %d:",Code);
  113.       SioError(Code);
  114.       SioDone(Port);
  115.       exit(1);
  116.      }
  117.  return(0);
  118. } /* end ErrorCheck */
  119.  
  120.  
  121. int BaudMatch(char *P)
  122. {int i;
  123.  /* find baud rate in table */
  124.  for(i=0;i<10;i++) if(strcmp(BaudRate[i],P)==0) return(i);
  125.  return(-1);
  126. }
  127.